//
//  MyTagView.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/27.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "MyTagView.h"
#import "TagSign.h"
#import "ZYButton.h"
#define kItemsBetweenWidth 18*([UIScreen mainScreen].bounds.size.width)/750
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface MyTagView ()<UITextFieldDelegate>
@property (nonatomic,strong) NSMutableArray *slectedArray;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UITextField *customSignField;
@property (nonatomic,retain) NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger itemBtnWidth;
@property (nonatomic,assign) NSInteger itemBtnHeight;
@property (nonatomic,strong) UIButton *sureBtn;
@end

@implementation MyTagView
- (id)initWithTitle:(NSString *)title contentTitleArray:(NSMutableArray *)contentArray frame:(CGRect)frame delegate:(id)delegate {
    if (self = [super initWithFrame:frame]) {
        _slectedArray = [[NSMutableArray alloc]initWithCapacity:0];
        _delegate     = delegate;
        _dataArr = contentArray;
        [self setUpSubViewsWithTitle:title tagArray:contentArray selectedTagArray:nil];
    }
    return self;
}


- (id)initWithTitle:(NSString *)title tagArray:(NSMutableArray *)tagArray selectedTagArray:(NSMutableArray *)selectedArray frame:(CGRect)frame delegate:(id)delagte {
    if (self = [super initWithFrame:frame]) {
        _slectedArray = [[NSMutableArray alloc]initWithCapacity:0];
        _delegate     = delagte;
        [self setUpSubViewsWithTitle:title tagArray:tagArray selectedTagArray:selectedArray];
    }
    return self;
}

- (void)setUpSubViewsWithTitle:(NSString *)title tagArray:(NSMutableArray *)array selectedTagArray:(NSMutableArray *)selectedArray {
    //    if (!_titleLabel) {
    //        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 50, 15)];
    //        _titleLabel.text = title;
    //        _titleLabel.textAlignment = NSTextAlignmentLeft;
    //        _titleLabel.font = [UIFont systemFontOfSize:13.0f];
    //    }
    //    [self addSubview:_titleLabel];
    
    _itemBtnWidth = (kScreenW - 3*kItemsBetweenWidth-60*kScale)/4;   //标签宽度
    _itemBtnHeight = 80*kScale;     //标签高度
    for (int i= 0;i<array.count;i++) {
        CGFloat x = (kItemsBetweenWidth +_itemBtnWidth)  * (i%4) + 30*kScale;
        CGFloat y = (kItemsBetweenWidth +_itemBtnHeight) * (i/4) + kItemsBetweenWidth;
        ZYButton *btn = [[ZYButton alloc]initWithFrame:CGRectMake(x, y, _itemBtnWidth, _itemBtnHeight)];
        if ([array[i] isKindOfClass:[NSDictionary class]]) {
            if ([self.tagTitle length] > 0) {
                NSDictionary *tempDic = array[i];
                [btn setTitle:tempDic[@"tag_name"] forState:UIControlStateNormal];
            } else {
                NSLog(@"如果使用的是Dictionary,请传入 key ");
                TagSign *tagSign = [[TagSign alloc]initWithDic:array[i]];;
                //                NSLog(@"%@",tagSign.name);
                btn.tagSign = tagSign;
                [btn setTitle:tagSign.name forState:UIControlStateNormal];
            }
        } else
        {
            TagSign *tagSign = [[TagSign alloc]initWithDic:array[i]];;
            NSLog(@"%@",tagSign.name);
            btn.tagSign = tagSign;

            [btn setTitle:tagSign.name forState:UIControlStateNormal];
        }
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn.backgroundColor = [UIColor whiteColor];
        [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        //        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.layer.cornerRadius  = 8*kScale;
        btn.layer.masksToBounds = YES;
        btn.tag = 10 +i;
        btn.layer.borderWidth = 1.0f;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [self addSubview:btn];
        if (i==array.count-1)
        {
            CGFloat fieldX = 30*kScale;
            CGFloat fieldY = (kItemsBetweenWidth +_itemBtnHeight) * (i/4+1) + kItemsBetweenWidth;
            _customSignField = [[UITextField alloc]initWithFrame:CGRectMake(fieldX, fieldY, _itemBtnWidth, _itemBtnHeight)];
            //            _customSignField.backgroundColor = [UIColor cyanColor];
            _customSignField.delegate =self;
            _customSignField.textAlignment = NSTextAlignmentCenter;
            _customSignField .font = [UIFont systemFontOfSize:14.0f];
            _customSignField.placeholder = @"自定义";
            _customSignField.layer.cornerRadius  = 8*kScale;
            _customSignField.layer.masksToBounds = YES;
            //            btn.tag = 10 +i;
            _customSignField.layer.borderWidth = 1.0f;
            _customSignField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            [self addSubview:_customSignField];
            _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            CGFloat sureX = kItemsBetweenWidth +_itemBtnWidth + 30*kScale;
            CGFloat sureY = (kItemsBetweenWidth +_itemBtnHeight) * (i/4+1) + kItemsBetweenWidth;
            _sureBtn.frame = CGRectMake(sureX, sureY, 78*kScale, 78*kScale);
            //            [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //            [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            //            sureBtn.backgroundColor = [UIColor whiteColor];
            [_sureBtn setImage:[UIImage imageNamed:@"选中自定义"] forState:UIControlStateNormal];
//            [_sureBtn setImage:[UIImage imageNamed:@"rest"] forState:UIControlStateSelected];
            [_sureBtn addTarget:self action:@selector(sureClicked:) forControlEvents:UIControlEventTouchUpInside];
            _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
            //        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//            _sureBtn.layer.cornerRadius  = 8*kScale;
//            _sureBtn.layer.masksToBounds = YES;
            _sureBtn.tag = 10+i;
//            _sureBtn.layer.borderWidth = 1.0f;
//            _sureBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
            [self addSubview:_sureBtn];
        }
    }
    //    if (selectedArray.count >0) {
    //        _slectedArray = [[NSMutableArray alloc]initWithArray:selectedArray];
    //        for (NSString *selectedTag in selectedArray) {
    //            UIButton *btn = [self viewWithTag:[selectedTag integerValue]];
    //            if (btn) {
    //                btn.selected = YES;
    //                btn.layer.borderWidth = 1.0f;
    //                btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //            }
    //        }
    //    }
}

- (void)itemBtnClicked:(ZYButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        //        sender.layer.borderWidth = 0.0f;
        sender.backgroundColor = [UIColor whiteColor];
//        NSString *tagString = [NSString stringWithFormat:@"%d",(int)sender.tag];
        TagSign *tagSign = sender.tagSign;
        if ([_slectedArray containsObject:tagSign]) {
            [_slectedArray removeObject:tagSign];
        }
    } else {
        sender.selected = YES;
        sender.layer.borderWidth = 1.0f;
        //        sender.layer.borderColor = [UIColor lightGrayColor].CGColor;
        sender.backgroundColor = [UIColor colorWithRed:15/255.0 green:162/255.0 blue:182/255.0 alpha:1.0f];
//        NSString *tagString = [NSString stringWithFormat:@"%d",(int)sender.tag];
        TagSign *tagSign = sender.tagSign;
        if (![_slectedArray containsObject:tagSign]) {
            [_slectedArray addObject:tagSign];
            NSLog(@"7777%@",tagSign.name);
        }
    }
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedTagArray:)]) {
        [_delegate didSelectedTagArray:_slectedArray.count >0 ? _slectedArray :@[]];
    }
    
}

- (void)sureClicked:(ZYButton*)sender
{
    NSLog(@"2222%@",_customSignField.text);
    if (![_customSignField.text isEqualToString:@""] ) {
        CGFloat x = (kItemsBetweenWidth +_itemBtnWidth)  * (_dataArr.count%4) + 30*kScale;
        CGFloat y = (kItemsBetweenWidth +_itemBtnHeight) * (_dataArr.count/4) + kItemsBetweenWidth;
        ZYButton *btn = [[ZYButton alloc]initWithFrame:CGRectMake(x, y, _itemBtnWidth, _itemBtnHeight)];
        NSMutableDictionary *tagDic = [[NSMutableDictionary alloc]init];
        [tagDic setValue:_customSignField.text forKey:@"tag_name"];
        [tagDic setValue:@"" forKey:@"id"];
        [_dataArr addObject:tagDic];
        TagSign *tagSign = [[TagSign alloc]initWithDic:tagDic];
        btn.tagSign = tagSign;

        [btn setTitle:tagSign.name forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn.selected = YES;
        btn.backgroundColor = [UIColor colorWithRed:15/255.0 green:162/255.0 blue:182/255.0 alpha:1.0f];
        [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        
        btn.layer.cornerRadius  = 8*kScale;
        btn.layer.masksToBounds = YES;
        btn.tag = _dataArr.count+10;
        btn.layer.borderWidth = 1.0f;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [_slectedArray addObject:tagSign];
        [self addSubview:btn];
        CGFloat fieldX = 30*kScale;
        CGFloat fieldY = (kItemsBetweenWidth +_itemBtnHeight) * ((_dataArr.count-1)/4+1) + kItemsBetweenWidth;
        _customSignField.frame = CGRectMake(fieldX, fieldY, _itemBtnWidth, _itemBtnHeight);
        _customSignField.text = nil;
        CGFloat sureX = kItemsBetweenWidth +_itemBtnWidth + 30*kScale;
        CGFloat sureY = (kItemsBetweenWidth +_itemBtnHeight) * ((_dataArr.count-1)/4+1) + kItemsBetweenWidth;
        _sureBtn.frame = CGRectMake(sureX, sureY, 78*kScale, 78*kScale);
    }
    
}


@end
